package com.fangyi.yunmusic.service.impl;

import com.fangyi.yunmusic.config.ResourceConfig;
import com.fangyi.yunmusic.exception.YunMusicException;
import com.fangyi.yunmusic.mapper.ArtistHasTagMapper;
import com.fangyi.yunmusic.mapper.ArtistMapper;
import com.fangyi.yunmusic.mapper.ArtistTagMapper;
import com.fangyi.yunmusic.pojo.Artist;
import com.fangyi.yunmusic.pojo.ArtistHasTag;
import com.fangyi.yunmusic.pojo.ArtistTag;
import com.fangyi.yunmusic.pojo.vo.ArtistVO;
import com.fangyi.yunmusic.pojo.vo.TagVO;
import com.fangyi.yunmusic.service.ArtistTagService;
import com.fangyi.yunmusic.utils.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.n3r.idworker.Id;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

import static java.util.stream.Collectors.toList;

@Service
public class ArtistTagServiceImpl implements ArtistTagService {
    @Autowired
    private ArtistMapper artistMapper;
    @Autowired
    private ArtistHasTagMapper artistHasTagMapper;
    @Autowired
    private ArtistTagMapper artistTagMapper;
    @Autowired
    private ResourceConfig resourceConfig;
    @Override
    public void addArtistTag(String tagName, String artistId) {
        if(artistMapper.selectByPrimaryKey(artistId)==null)
            throw new YunMusicException("歌手ID不存在！");
        Example example=new Example(ArtistTag.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("name",tagName);
        List<ArtistTag> playlistTags=artistTagMapper.selectByExample(example);
        if(playlistTags.size()==1)
            throw new YunMusicException("歌手标签名称已存在");
        String tagId=Id.nextString();
        ArtistTag artistTag=new ArtistTag();
        artistTag.setId(tagId);
        artistTag.setName(tagName);
        artistTagMapper.insert(artistTag);
        String artistHasTagId=Id.nextString();
        ArtistHasTag artistHasTag=new ArtistHasTag();
        artistHasTag.setId(artistHasTagId);
        artistHasTag.setArtistTagId(tagId);
        artistHasTag.setArtistId(artistId);
        artistHasTagMapper.insert(artistHasTag);
    }

    @Override
    public void removeArtistTag(String tagId) {
        if(artistTagMapper.selectByPrimaryKey(tagId)==null)
            throw new YunMusicException("歌手标签ID不存在");
        Example example=new Example(ArtistHasTag.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("artistTagId",tagId);
        artistHasTagMapper.deleteByExample(example);
        artistTagMapper.deleteByPrimaryKey(tagId);
    }

    @Override
    public List<TagVO> getAllArtistTagVOs() {
        return   artistTagMapper.selectAll().stream().map(PlaylistTag->{
            TagVO tagVO=new TagVO();
            BeanUtils.copyProperties(PlaylistTag,tagVO);
            return  tagVO;

        }).collect(toList());
    }

    @Override
    public PagedResult getArtistVOsByTagId(String tagId, Integer page, Integer pageSize) {
        if(tagId!=null&&artistTagMapper.selectByPrimaryKey(tagId)==null)
            throw new YunMusicException("歌手标签ID不存在");
        Example example=new Example(ArtistHasTag.class);
        Example.Criteria criteria=example.createCriteria();
        criteria.andEqualTo("artistTagId",tagId);
        List<String> artistIds= artistHasTagMapper.selectByExample(example).stream().map(ArtistHasTag::getArtistId).collect(toList());
        Example example1=new Example(Artist.class);
        Example.Criteria criteria1=example1.createCriteria();
        criteria1.andIn("id",artistIds);
        PageHelper.startPage(page,pageSize);
        List<Artist> artists=artistMapper.selectByExample(example1);
        List<ArtistVO> artistVOS=artists.stream().map(Artist->{
            ArtistVO artistVO=new ArtistVO();
            BeanUtils.copyProperties(Artist,artistVO);
            artistVO.setPicUrl(resourceConfig.getArtistPicUrl(Artist.getPicUrlId()));
            return artistVO;


        }).collect(toList());
        PageInfo<?> pageInfo = new PageInfo<>(artists);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        return new PagedResult(artistVOS,page,pages,total);
    }
}
